package com.hariro.fragment;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.project.R;

public class Shake_Fragment extends Fragment {

	private RotateAnimation animation_0 = null;
	private RotateAnimation animation_1 = null;
	private RotateAnimation animation_2 = null;
	private ImageView imageView_shake_pic;
	private TextView textView_shake_hint;
	private SensorManager sensorManager = null;
	private Sensor sensor = null;
	private Vibrator mVibrator = null;
	private SoundPool soundPool = null;
	private SoundPool soundPool_After = null;
	private int soundId = 0;
	private int soundId_After = 0;
	private SensorEventListener listener = new SensorEventListener() {

		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub
			float valueX = event.values[0];
			float valueY = event.values[1];
			float valueZ = event.values[2];

			if (valueX > 12 || valueY > 12 || valueZ > 12) {
				startAnimator();
				startVibrator();
				startSound();

			}
		}

		private void startSound() {
			// 开启声音
			soundPool.play(soundId, 1, 1, 1, 0, 1);
		}

		private void startVibrator() {
			// 开启震动;
			mVibrator.vibrate(new long[] { 500, 200, 500, 200 }, -1);
		}

		private void startAnimator() {
			// 开启动画;
			animation_0 = new RotateAnimation(0f, -40f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation_0.setDuration(500);
			animation_1 = new RotateAnimation(-40f, 20f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation_1.setDuration(1000);
			animation_2 = new RotateAnimation(20f, 0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation_1.setDuration(500);

			animation_0.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
					// 在第一个动画开始播放的时候,解除监听器,以防止"无限摇一摇"的情况;
					sensorManager.unregisterListener(listener);
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					// 当第一个动画播放完毕播放第二个
					imageView_shake_pic.startAnimation(animation_1);
				}
			});

			animation_1.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					// 当第二个动画播放完毕播放第三个
					imageView_shake_pic.startAnimation(animation_2);
				}
			});

			animation_2.setAnimationListener(new AnimationListener() {

				@Override
				public void onAnimationStart(Animation animation) {
				}

				@Override
				public void onAnimationRepeat(Animation animation) {
				}

				@Override
				public void onAnimationEnd(Animation animation) {
					// 播放完动画隐藏文字;
					textView_shake_hint.setVisibility(View.GONE);

					// 发送一个延时意图,俩秒之后弹出吐司,播放音效,显示文字,再次注册监听器;
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							Toast.makeText(getActivity(), "什么也没摇到，再试试吧",
									Toast.LENGTH_LONG).show();
							mVibrator.cancel();
							soundPool_After.play(soundId_After, 1, 1, 1, 0, 1);
							textView_shake_hint.setVisibility(View.VISIBLE);
							sensorManager.registerListener(listener, sensor,
									SensorManager.SENSOR_DELAY_NORMAL);
						}
					}, 2000);
				}
			});

			imageView_shake_pic.startAnimation(animation_0);
		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_shake, null);
		imageView_shake_pic = (ImageView) view
				.findViewById(R.id.imageView_shake_pic);
		textView_shake_hint = (TextView) view
				.findViewById(R.id.textView_shake_hint);

		// 初始化设备;
		initSensor();

		// 初始化震动频率;
		initVibrator();

		// 初始化声音池;
		initSoundPool();

		return view;
	}

	private void initSoundPool() {
		// 初始化声音池;
		soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
		soundId = soundPool.load(getActivity(), R.raw.sound_shake, 1);

		soundPool_After = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
		soundId_After = soundPool_After.load(getActivity(),
				R.raw.sound_shake_none, 1);
	}

	private void initVibrator() {
		// 初始化震动;
		mVibrator = (Vibrator) getActivity().getSystemService(
				getActivity().VIBRATOR_SERVICE);
	}

	private void initSensor() {
		// 初始化传感器;
		sensorManager = (SensorManager) getActivity().getSystemService(
				getActivity().SENSOR_SERVICE);
		sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		sensorManager.registerListener(listener, sensor,
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	public void unRegisterListener() {
		if (sensorManager != null) {
			sensorManager.unregisterListener(listener);
		}

		if (soundPool != null) {
			soundPool.release();
			soundPool = null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// 在Activity销毁的同时,销毁传感器信息和声音池;
		if (sensorManager != null) {
			sensorManager.unregisterListener(listener);
			mVibrator.cancel();
		}

		if (soundPool != null) {
			soundPool.release();
			soundPool = null;
		}
		getActivity().finish();
	}
}
